home *** CD-ROM | disk | FTP | other *** search
- #include <menus.h>
- #include <types.h>
- #include <memory.h>
-
-
- void SetMenuTitle (MenuHandle theMenu, Str255 theNewTitle);
-
-
- void SetMenuTitle (MenuHandle theMenu, Str255 theNewTitle)
- {
- Byte oldTitleLength, newTitleLength;
- Ptr oldItemLocation, newItemLocation;
- Size oldHandleSize, newHandleSize;
- Ptr theMenuAsAPtr;
- Ptr theDataAsAPtr;
- Size toMove;
- short difference;
- char theOldState;
-
- theOldState = HGetState((Handle)theMenu); /* get the old menu handle state */
-
- HLock((Handle)theMenu); /* lock it */
- theMenuAsAPtr = (Ptr)*theMenu; /* offset to the menu data area is based on the */
- theDataAsAPtr = (Ptr)&(**theMenu).menuData; /* MenuInfo structure in Menus.h and Inside Mac */
-
- oldTitleLength = theDataAsAPtr[0];
- newTitleLength = theNewTitle[0];
-
- difference = newTitleLength - oldTitleLength;
-
- oldHandleSize = GetHandleSize((Handle)theMenu);
- newHandleSize = oldHandleSize + difference;
-
- /* If your titles are not all the same length, (i.e. difference != 0) */
- /* then you are going to have to shift the items in the menu around, */
- /* since the Menu Manager figures out where the items start based on string lengths */
-
-
- if (difference > 0)
- {
- /* we have to grow the handle, so call SetHandleSize() then */
- /* move the menu item date to make room for the longer title */
-
- HUnlock((Handle)theMenu);
- SetHandleSize((Handle)theMenu, newHandleSize);
-
- /* check memerror here */
-
- HLock((Handle)theMenu);
- theMenuAsAPtr = (Ptr)*theMenu;
- theDataAsAPtr = (Ptr)&(**theMenu).menuData;
-
- /* here, we get the location of the actual menu items */
- /* by going past the header and the current title string */
-
- oldItemLocation = theDataAsAPtr + oldTitleLength + 1;
- newItemLocation = theDataAsAPtr + newTitleLength + 1;
- toMove = oldHandleSize - (oldItemLocation - theMenuAsAPtr);
- BlockMove(oldItemLocation,newItemLocation,toMove);
- }
-
- if (difference < 0)
- {
- /* we have to shrink the handle, so move the data first */
-
- oldItemLocation = theDataAsAPtr + oldTitleLength + 1;
- newItemLocation = theDataAsAPtr + newTitleLength + 1;
- toMove = oldHandleSize - (oldItemLocation - theMenuAsAPtr);
- BlockMove(oldItemLocation,newItemLocation,toMove);
-
- /* now we can resize the menu handle */
-
- HUnlock((Handle)theMenu);
- SetHandleSize((Handle)theMenu, newHandleSize);
-
- /* check memerror here */
-
- HLock((Handle)theMenu);
- theMenuAsAPtr = (Ptr)*theMenu;
- theDataAsAPtr = (Ptr)&(**theMenu).menuData;;
- }
-
- /* move the new title into place */
-
- BlockMove((Ptr)theNewTitle, theDataAsAPtr, newTitleLength+1);
-
- HSetState((Handle)theMenu,theOldState); /* restore the original handle state */
-
- /* to insure redrawing, set and reset the menu bar */
-
- SetMenuBar(GetMenuBar());
- DrawMenuBar();
- }
-